package com.xgkj.diyiketang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AddFriendTask;
import com.xgkj.diyiketang.task.AddOrCancleFocusTask;
import com.xgkj.diyiketang.task.DeleteFriendsTask;
import com.xgkj.diyiketang.task.GetUserInfoTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ShareUtil;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class HisDetailActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout titleLayout;
    private TextView back;
    private RelativeLayout myInfo;
    private RoundImageView headImg;
    private RelativeLayout relative1;
    private ImageView guanzhuBtn;
    private TextView nickname;
    private RelativeLayout personSignatureLayout;
    private TextView qianming;
    private TextView personSignature;
    private RelativeLayout schoolLayout;
    private TextView school;
    private TextView schoolText;
    private RelativeLayout attentionLayout;
    private RelativeLayout rijiLayout;
    private RelativeLayout kechengLayout;
    private RelativeLayout timuLayout;
    private Button sendMsgBtn;
    private Button addFriendBtn;
    private Button deleteFriendBtn;
    private String mUserId;
    private int userDetailTaskId = -1;
    private EatShowUserInfo mUserInfo;
    private int addFriendTaskId = -1;
    private int deleteFriendTaskId = -1;
    private int cancleFocusTaskId = -1;
    private ImageView mShare;
    private String title;
    private ImageView lastLine;
    private ImageView lastSecondLine;
    private EatShowUserInfo selfInfo;
    private String selfUserId;
    private String headLogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_his_detail);
        mContext = this;
        initView();

    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            mUserId = intent.getStringExtra("USERID");
        }

        if (mUserId == null) {
            finish();
            return;
        }
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);

        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(this);

        mShare = (ImageView) findViewById(R.id.share);
        mShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String share_title = title;
                String share_url = "http://115.28.236.67:8096/classroom/teacher_detail.html?userId=" + mUserId + "";
                ShareUtil.getInstance(mContext).show(share_title, share_url, new PopupWindow.OnDismissListener() {
                            @Override
                            public void onDismiss() {

                            }
                        },
                        mShare);
            }
        });
        myInfo = (RelativeLayout) findViewById(R.id.my_info);
        headImg = (RoundImageView) findViewById(R.id.head_img);
        headImg.setOnClickListener(this);
        relative1 = (RelativeLayout) findViewById(R.id.relative1);
        guanzhuBtn = (ImageView) findViewById(R.id.guanzhu_btn);
        guanzhuBtn.setOnClickListener(this);
        nickname = (TextView) findViewById(R.id.nickname);
        personSignatureLayout = (RelativeLayout) findViewById(R.id.person_signature_layout);
        qianming = (TextView) findViewById(R.id.qianming);
        personSignature = (TextView) findViewById(R.id.person_signature);
        schoolLayout = (RelativeLayout) findViewById(R.id.school_layout);
        school = (TextView) findViewById(R.id.school);
        schoolText = (TextView) findViewById(R.id.school_text);
        attentionLayout = (RelativeLayout) findViewById(R.id.attention_layout);
        attentionLayout.setOnClickListener(this);
        rijiLayout = (RelativeLayout) findViewById(R.id.riji_layout);
        rijiLayout.setOnClickListener(this);
        kechengLayout = (RelativeLayout) findViewById(R.id.kecheng_layout);
        kechengLayout.setOnClickListener(this);
        timuLayout = (RelativeLayout) findViewById(R.id.timu_layout);
        timuLayout.setOnClickListener(this);
        addFriendBtn = (Button) findViewById(R.id.addfriend_btn);
        addFriendBtn.setOnClickListener(this);
        sendMsgBtn = (Button) findViewById(R.id.sendmsg_btn);
        sendMsgBtn.setOnClickListener(this);
        deleteFriendBtn = (Button) findViewById(R.id.delete_btn);
        deleteFriendBtn.setOnClickListener(this);
        lastLine = (ImageView) findViewById(R.id.last_line);
        lastSecondLine = (ImageView) findViewById(R.id.last_second_line);
        selfInfo = EatShowApp.getInstance().getmUserInfo();
        if (selfInfo != null) {
            selfUserId = selfInfo.getUserId();
        }
        getUserDetail();
    }

    private void getUserDetail() {
        GetUserInfoTask task = new GetUserInfoTask(mUserId);
        task.addListener(myListener);
        userDetailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void addFriend(String userId) {
        AddFriendTask task = new AddFriendTask(userId);
        task.addListener(myListener);
        addFriendTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void deleteFriend(String userId) {
        DeleteFriendsTask task = new DeleteFriendsTask(userId);
        task.addListener(myListener);
        deleteFriendTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void doAttention(String userId, String type) {
        AddOrCancleFocusTask task = new AddOrCancleFocusTask(userId, type);
        task.addListener(myListener);
        cancleFocusTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener myListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (userDetailTaskId == taskId) {
                if (data != null) {
                    mUserInfo = (EatShowUserInfo) data;
                }
                updateUserInfo();
            } else if (taskId == addFriendTaskId) {
                ToastUtil.showMessage(mContext, R.string.apply_to_friend_alreay_send);
                getUserDetail();
            } else if (taskId == deleteFriendTaskId) {
                ToastUtil.showMessage(mContext, R.string.delete_friend_success);
                getUserDetail();
            } else if (taskId == cancleFocusTaskId) {
                if (mUserInfo.isFocus()) {
                    guanzhuBtn.setImageResource(R.drawable.yiguanzhu);
                    mUserInfo.setIsFocus(false);
                } else {
                    guanzhuBtn.setImageResource(R.drawable.weiguanzhu);
                    mUserInfo.setIsFocus(true);
                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }
    };

    private void updateUserInfo() {
        if (mUserInfo == null) return;
        mUserId = mUserInfo.getUserId();
        headLogo = mUserInfo.getUserLogo();
        if (headLogo != null && headLogo.length() > 0) {
            ImageCache.getInstance(mContext).loadBitmapToView(headImg, headLogo, ViewUtil.getHeadImageSize(mContext, 65));
        }

        String nickName = null;
        try {
            nickName = URLDecoder.decode(mUserInfo.getNickName(), "utf-8");
            title = nickName;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        nickname.setText(nickName);


        String qianming = null;
        try {
            qianming = URLDecoder.decode(mUserInfo.getUserSign(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        personSignature.setText(qianming);

        schoolText.setText(mUserInfo.getSchoolName());

        if (mUserInfo.getUserType() == 1) {
            kechengLayout.setVisibility(View.VISIBLE);
            lastSecondLine.setVisibility(View.VISIBLE);
            lastSecondLine.setVisibility(View.VISIBLE);
            timuLayout.setVisibility(View.VISIBLE);
            guanzhuBtn.setVisibility(View.VISIBLE);
        } else if (mUserInfo.getUserType() == 0) {
            kechengLayout.setVisibility(View.GONE);
            lastSecondLine.setVisibility(View.GONE);
            lastLine.setVisibility(View.GONE);
            timuLayout.setVisibility(View.GONE);
            guanzhuBtn.setVisibility(View.GONE);
        }

        if (mUserInfo.isFriend()) {
            sendMsgBtn.setVisibility(View.VISIBLE);
            deleteFriendBtn.setVisibility(View.VISIBLE);
            addFriendBtn.setVisibility(View.GONE);
        } else {
            addFriendBtn.setVisibility(View.VISIBLE);
            sendMsgBtn.setVisibility(View.GONE);
            deleteFriendBtn.setVisibility(View.GONE);
        }

        if (mUserId.equals(selfUserId)) {
            addFriendBtn.setVisibility(View.GONE);
            sendMsgBtn.setVisibility(View.GONE);
            deleteFriendBtn.setVisibility(View.GONE);
            guanzhuBtn.setVisibility(View.GONE);
        }

        if (!mUserInfo.isFocus) {
            //关注
            guanzhuBtn.setImageResource(R.drawable.yiguanzhu);
        } else {
            //没关注
            guanzhuBtn.setImageResource(R.drawable.weiguanzhu);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addfriend_btn:
                if (mUserId == null) return;
                addFriend(mUserId);
                break;
            case R.id.sendmsg_btn:

                String Uid = mUserInfo.getUserId().toLowerCase();
                String opid = mUserInfo.getOpenId();
                if (opid != null && opid.length() > 1) {
                    Uid = opid.toLowerCase();
                }

                EatShowUserInfo myInfo = EatShowApp.getInstance().getmUserInfo();
                String myId = TextUtils.isEmpty(myInfo.getOpenId()) ? myInfo.getUserId() : myInfo.getOpenId();
                if (myId.equals(Uid)) {
                    ToastUtil.showMessage(mContext, R.string.Cant_chat_with_yourself);
                    return;
                }

                Intent intent = new Intent(mContext, ChatActivity.class);
                intent.putExtra("userId", Uid);
                intent.putExtra("userNick", mUserInfo.getNickName());
                intent.putExtra("myLogo", EatShowApp.getInstance().getmUserInfo().getUserLogo());
                intent.putExtra("fromLogo", mUserInfo.getUserLogo());
                intent.putExtra("userType", mUserInfo.getUserType());
                mContext.startActivity(intent);
                break;
            case R.id.delete_btn:

                if (mUserId == null) return;
                deleteFriend(mUserId);
                break;
            case R.id.riji_layout:
                Intent riji_intent = new Intent(mContext, MyRiJiActivity.class);
                riji_intent.putExtra("USERID", mUserId);
                riji_intent.putExtra("NICKNAME", mUserInfo.getNickName());
                riji_intent.putExtra("HEADLOGO", mUserInfo.getUserLogo());
                startActivity(riji_intent);

                break;
            case R.id.attention_layout:
                Intent attention_intent = new Intent(mContext, MyAttentionActivity.class);
                attention_intent.putExtra("USERID", mUserId);
                startActivity(attention_intent);

                break;
            case R.id.kecheng_layout:
                Intent kecheng_intent = new Intent(mContext, MyVideoListActivity.class);
                kecheng_intent.putExtra("USERID", mUserId);
                startActivity(kecheng_intent);

                break;
            case R.id.timu_layout:
                Intent timu_intent = new Intent(mContext, MyQuestionListActivity.class);
                timu_intent.putExtra("USERID", mUserId);
                startActivity(timu_intent);
                break;
            case R.id.guanzhu_btn:
                if (mUserInfo.isFocus()) {
                    doAttention(mUserId, "0");
                } else {
                    doAttention(mUserId, "1");
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.head_img:
                Intent bintent = new Intent(mContext, SingleBigImageActivity.class);
                bintent.putExtra("BIGIMAGE", headLogo);
                startActivity(bintent);
                break;
        }
    }
}
